var { Tray, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
let url = path.join(__dirname, '../../static/images/favicon.ico')
var appIcon = new Tray(url);
appIcon.setToolTip('music app');

let win = BrowserWindow.fromId(global.mainId)

let trayWindow

trayWindow = new BrowserWindow({
    height: 0,
    useContentSize: true,
    width: 0,
    backgroundColor: '#FFFFFFFF',
    alwaysOnTop: true, //窗口置顶
    frame: false,  //最大化，最小化，关闭按钮
    resizable: false, //禁止改变主窗口尺寸
    webPreferences: {
        nodeIntegration: true,
        webSecurity: false
    }
})
global.trayId = trayWindow.id  //存储窗口id
trayWindow.hide()  //隐藏窗口
trayWindow.setMenu(null)  //取消菜单栏 
trayWindow.loadURL(global.winURL + "#/tray")
trayWindow.webContents.closeDevTools()  //关闭调试工具
// 失去焦点时关闭托盘窗口
trayWindow.on("blur", () => {
    trayWindow.hide()
})
trayWindow.on('closed', () => {
    trayWindow = null
})

ipcMain.on("closeTray", () => {
    trayWindow.close()
})

// 双击显示主页面
appIcon.on("double-click", () => {
    win.show()
})
// 鼠标右击显示托盘窗口
appIcon.on("right-click", (e) => {
    e.preventDefault()
    let { x, y } = appIcon.getBounds()
    trayWindow.setBounds({ "x": x + 25, "y": y - 290, "height": 300, "width": 180 })
    // trayWindow.setBounds({ "x": x + 25 - 300, "y": y - 290, "height": 300, "width": 480 })
    trayWindow.show()
})


